<template>
  <div>
    <div class="search">
      <Row>
        <Col>
          <Card>
            <Row>
              <Form ref="searchForm" :model="searchForm" inline :label-width="100" class="search-form">
                <FormItem label="项目部" prop="my_organization_id" >
                  <Select v-model="searchForm.my_organization_id" clearable filterable  @on-change="getOrganizationId">
                    <Option v-for="item in organizationList" :value="item.id" :key="item.id">{{ item.organization_name }}</Option>
                  </Select>
                </FormItem>
                <Form-item style="margin-left:-100px;" class="br">
                  <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                  <Button @click="handleReset">重置</Button>
                </Form-item>
              </Form>
            </Row>
            <Row>
              <Alert show-icon>
                已选择 <span class="select-count">{{selectCount}}</span> 项
                <a class="select-clear" @click="clearSelectAll">清空</a>
              </Alert>
            </Row>
            <Row>
              <Table
                border
                :loading="loading"
                :columns="columns"
                :data="data"
                sortable="custom"
                @on-selection-change="showSelect"
                ref="table">
              </Table>
              <Table :columns="exportColumns" :data="exportData" ref="exportTable" style="display:none"></Table>
            </Row>
            <Row type="flex" justify="end" class="page">
              <Page
                :current="searchForm.pageNumber"
                :total="total"
                :page-size="searchForm.pageSize"
                @on-change="changePage"
                @on-page-size-change="changePageSize"
                :page-size-opts="[10,20,50]"
                size="small"
                show-total
                show-elevator
                show-sizer
              ></Page>
            </Row>
          </Card>
        </Col>
      </Row>
    </div>  
  </div>
</template>
<script>
import { getOrganization, getDivisionBudget } from '@/api/contract';
import { base_url } from '@/libs/axios';
export default {
  name: 'budget-approval',
  data() {
    return {
      loading: false,
      searchForm: {
        pageNumber: 1,
        pageSize: 10,
        my_organization_id: 0
      },
      organizationList: [],
      data: [],
      total: 0,
      selectCount: 0,
      exportData: [],
      selectList: [],
      exportColumns: [],
      columns: [
        {
          type: "selection",
          width: 50,
          align: "center",
          fixed: "left"
        },
        {
          type: "index",
          width: 50,
          align: "center",
          fixed: "left"
        },
        {
          title: '项目部',
          key: 'organization_name',
          width: 100
        },
        {
          title: '项目编号',
          key: 'project_code',
          width: 160,
          align: 'center'
        },
        {
          title: '项目名称',
          key: 'project_name',
          width: 220,
        },
        {
          title: '设备费(含税价)',
          key: 'equipment_cost',
          width: 120,
          align: 'right'
        },
        {
          title: '主材费(含税价)',
          key: 'material_cost',
          width: 120,
          align: 'right'
        },
        {
          title: '预算造价',
          key: 'budget_cost',
          width: 120,
          align: 'right'
        },
        {
          title: '预算员',
          key: 'real_name',
          width: 80,
          align: 'center'
        },
        {
          title: '编制日期',
          key: 'budget_date',
          width: 120,
          align: 'center'
        },
        {
          title: '预算书',
          key: 'budget_content',
          width: 60,
          align: 'center',
          render: (h, params) => {
            if (params.row.budget_content) {
              return h(
                "a",
                {
                  on: {
                    click: () => {
                      this.showDetail(params.row);
                    }
                  }
                },
                '预算书'
              );
            }
          }
        },
      ],
    }
  },
  methods: {
    getOrganizations() {
      getOrganization().then(res => {
        if (res.code === 0) {
          this.organizationList = res.data;
        }
      })
    },
    getBudgetList() {
      getDivisionBudget(this.searchForm).then(res => {
        console.log(res);
        if (res.code === 0) {
          this.data = res.data.rows;
          this.total = res.data.count;
        }
      })
    },
    init() {
      this.getOrganizations();
      this.getBudgetList();
    },
    getOrganizationId(v) {
      this.searchForm.my_organization_id = v == undefined ? 0 : v;
    },
    handleSearch() {
      if (this.searchForm.my_organization_id == 0) {
        this.$Message.info('请选择要查询的项目部');
        return;
      } else {
        this.getBudgetList();
      }
    },
    handleReset() {
      this.searchForm.my_organization_id = 0;
      this.pageNumber = 1;
      this.pageSize = 10;
      this.getBudgetList();
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    showSelect(e) {
      this.exportData = e;
      this.selectList = e;
      this.selectCount = e.length;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getBudgetList()
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getBudgetList();
    },
    showDetail(v) {
      if (v.budget_content) {
        window.open(`${base_url}/public/uploads/${v.budget_content}`);
      } else {
        this.$Message.info('请先上传预算书到系统中！');
        return
      }
    },
  },
  mounted() {
    this.init();
  }
}
</script>
<style lang="less" scoped>
@import "./budgetApproval.less";
</style>